/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jm.jmsql.cloud.persitence;

import com.jm.jmsql.cloud.entities.Users;
import java.util.GregorianCalendar;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 *
 * @author Marques
 */
public class UserMaintenance {
    
    /**
     * 
     */
    private static UserMaintenance userMaintenance;
    private static SessionFactory sessionFactory;
    
    /**
     * 
     */
    private UserMaintenance() {
        Configuration configuration = new Configuration().configure();
	ServiceRegistryBuilder builder = new ServiceRegistryBuilder().applySettings(configuration.getProperties());
        sessionFactory = new Configuration().configure().buildSessionFactory(builder.buildServiceRegistry());
    }
    
    /**
     * 
     * @return 
     */
    public static UserMaintenance getInstance() {
        if (userMaintenance == null) {
            userMaintenance = new UserMaintenance();
        }
        return userMaintenance;
    }
    
    /**
     * 
     * @param email
     * @return 
     */
    public Users getUserByEmail(String email) {
        Session session = sessionFactory.openSession();
        
        try {
            session.beginTransaction();
            
            Criteria criteria = session.createCriteria(Users.class);
            criteria.add(Restrictions.eq("email", email));
            
            return (Users) criteria.uniqueResult();
        } finally {
            session.close();
        }
    }
    
    /**
     * 
     * @param username
     * @return Users
     */
    public Users getUserByUsername(String username) {
        Session session = sessionFactory.openSession();
        
        try {
            session.beginTransaction();
            
            Criteria criteria = session.createCriteria(Users.class);
            criteria.add(Restrictions.eq("username", username));
            
            return (Users) criteria.uniqueResult();
        } finally {
            session.close();
        }
    }
    
    /**
     * 
     * @param user
     * @return 
     */
    public boolean addUser(Users user) {
        Session session = sessionFactory.openSession();
        try {
            Transaction transaction = session.beginTransaction();
            
            user.setCreationDate(GregorianCalendar.getInstance().getTime());
            user.setCreationTime(GregorianCalendar.getInstance().getTime());
            user.setLoginDate(GregorianCalendar.getInstance().getTime());
            user.setLoginTime(GregorianCalendar.getInstance().getTime());
            
            session.save(user);
            
            transaction.commit();
            return true;
        } finally {
            session.close();
        }
    }
    
}
